<template>
  <div>
    <router-link tag="div" to="/" v-show="showAbs" class="header-abs">
      <span class="iconfont header-abs-iconback">&#xe624;</span>
    </router-link>
    <div class="header-fixed" v-show="!showAbs" :style="opacityStyle">
      <router-link to="/">
        <span class="iconfont header-fixed-iconback">&#xe624;</span>
      </router-link>
      <h1 class="header-title">景点详情</h1>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  data: function () {
    return {
      showAbs: true,
      opacityStyle: { opacity: 0 }
    }
  },
  methods: {
    handleScroll: function () {
      // 距离顶部的值
      const top = document.documentElement.scrollTop
      // dom的透明度只能定义成opacity
      if (top > 30) {
        let opacity = top / 210
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = { opacity }
        this.showAbs = false
      } else {
        this.showAbs = true
      }
    }
  },
  // 新的页面展示的时候，这个钩子就会执行
  activated: function () {
    // scroll是一个全局事件绑定，对其他组件也产生作用 ，scroll是滚动事件
    window.addEventListener('scroll', this.handleScroll)
  },
  // 页面被隐藏或替换成新的页面，这个钩子会执行
  deactivated: function () {
    // scroll全局滚动事件只对该组件的handleScroll函数作用
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="stylus" scoped>
@import '~@/assets/styles/varibles.styl';

.header-abs {
  /* *可以放置到页面的任何位置 */
  position: absolute;
  left: 0.2rem;
  top: 0.2rem;
  width: 0.8rem;
  height: 0.8rem;
  line-height: 0.8rem;
  text-align: center;
  border-radius: 0.4rem;
  opacity: 0.7;
  background: rgba(0, 0, 0, 0.8);

  .header-abs-iconback {
    color: #ffffff;
    font-size: 0.4rem;
  }
}

.header-fixed {
  /* 覆盖父级元素属性 */
  z-index: 1;
  position: fixed;
  top: 0rem;
  left: 0rem;
  right: 0;
  background: $bgColor;
  height: $headerHeight;
  line-height: $headerHeight;

  .header-fixed-iconback {
    left: 0;
    top: 0;
    width: 0.8rem;
    height: $headerHeight;
    line-height: $headerHeight;
    position: absolute;
    font-size: 0.4rem;
    color: #fff;
    text-align: center;
  }

  .header-title {
    overflow: hidden;
    margin: 0 1rem;
    line-height: $headerHeight;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 0.32rem;
    text-align: center;
    color: #fff;
  }
}
</style>
